/**
 *
 */
package com.storm.lib.generator;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;


import org.apache.log4j.Logger;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.stereotype.Controller;

import com.storm.lib.base.BaseJavaExecuter;
import com.storm.lib.bean.DataCellBean;
import com.storm.lib.bean.DataRowBean;
import com.storm.lib.bean.SheetBean;
import com.storm.lib.type.SystemType;
import com.storm.lib.util.DBUtil;
import com.storm.lib.util.ExcelUtil;
import com.storm.lib.util.ExceptionUtil;
import com.storm.lib.util.FileUtil;
import com.storm.lib.util.FormatUtil;
import com.storm.lib.util.StringUtil;


/**
 * 类功能: 用来创建PO
 * @author johnny
 * @version 2010-10-27
 */
@Controller
public class PoBuilder extends BaseJavaExecuter{
	protected final Logger logger = Logger.getLogger(getClass());
	private static PoBuilder pOBuilder;

	public static PoBuilder getInstance(){
		if(pOBuilder==null){
			pOBuilder = new PoBuilder();
		}
		return pOBuilder;
	}

	public void execute(String sheetName) {

		StringBuffer sb = new StringBuffer();
		HSSFWorkbook workBook = FileUtil.getGameExcelData();
		for (int is = 0; is < workBook.getNumberOfSheets(); is++) {
			HSSFSheet sheet = workBook.getSheetAt(is);
			if(sheet.getSheetName().contains("Tag-structure-")==false){
				continue;
			}
			if(sheet.getSheetName().equals(sheetName)==false){
				continue;
			}
			SheetBean sheetBean = ExcelUtil.getSheetBeanBySheet(sheet,SystemType.LANGUAGE_PREFIX_SIMPLIFIED_CHINESE);
			String template="";
			String db = (String)sheetBean.getParameters().get(0);
			String table = (String)sheetBean.getParameters().get(1);
			String basePo = "BaseUserDBPo";
			String pack = "";
			String fileName = "";
			if("rpg_user_db".equals(db)){
				db = "SystemType.USER_DB_NAME";
			}
			else if("rpg_game_db".equals(db)){
				db = "SystemType.GAME_DB_NAME";
				basePo = "BaseGameDBPo";
				pack = ".game";
				fileName = "game\\";
			}
			String classComment = (String)sheetBean.getParameters().get(2);
			String author = (String)sheetBean.getParameters().get(3);
			String date = (String)sheetBean.getParameters().get(4);
			String className = (String)sheetBean.getParameters().get(5);
//			sheetBean.printSelf();
			List<DataRowBean> rowList = sheetBean.getRows();
			String headerStr="";
			String columnStr="";
			String functionStr="";

			headerStr+=FormatUtil.getNString("package com.games.rpg.po"+pack+";\n");
			headerStr+=FormatUtil.getNString("import java.sql.Timestamp;");
			headerStr+=FormatUtil.getNString("import java.util.Date;");
			headerStr+=FormatUtil.getNString("import javax.persistence.Column;");
			headerStr+=FormatUtil.getNString("import javax.persistence.Entity;");
			headerStr+=FormatUtil.getNString("import javax.persistence.FetchType;");
			headerStr+=FormatUtil.getNString("import javax.persistence.GeneratedValue;");
			headerStr+=FormatUtil.getNString("import javax.persistence.Id;");
			headerStr+=FormatUtil.getNString("import javax.persistence.JoinColumn;");
			headerStr+=FormatUtil.getNString("import javax.persistence.ManyToOne;");
			headerStr+=FormatUtil.getNString("import javax.persistence.Table;");
			headerStr+=FormatUtil.getNString("import com.storm.lib.base."+basePo+";");
			headerStr+=FormatUtil.getNString("import com.storm.lib.type.SystemType;");

			headerStr+=FormatUtil.getTNString("/**");
			headerStr+=FormatUtil.getTNString(" *");
			headerStr+=FormatUtil.getTNString(" * 类功能: "+classComment);
			headerStr+=FormatUtil.getTNString(" *");
			headerStr+=FormatUtil.getTNString(" * @author "+author);
			headerStr+=FormatUtil.getTNString(" * @version "+date);
			headerStr+=FormatUtil.getTNString(" */");

			headerStr+=FormatUtil.getTNString("@Entity");
			headerStr+=FormatUtil.getTNString("@Table(name = \""+table+"\",catalog="+db+")");
			headerStr+=FormatUtil.getTNString("public class "+className+" extends "+basePo+" {");

			columnStr+=("\n\t/**");
			columnStr+=("\n\t*主键");
			columnStr+=("\n\t**/\n");
			columnStr+=("\n\tprivate Integer id;");

		    functionStr+=String.format("\n\n\t@Id @GeneratedValue");
			functionStr+=String.format("\n\n\t@Column(name=\""+"id"+"\", unique=true, nullable=false)");
			functionStr+=String.format("\n\t public "+"Integer"+" getId() {");
			functionStr+=String.format("\n\t\t%s","return this."+"id"+";");
			functionStr+=String.format("\n\t}");
			functionStr+=String.format("\n\tpublic void setId(Integer id) {");
			functionStr+=String.format("\n\t\t%s","this.id = id;");
			functionStr+=String.format("\n\t}");
			for (int i = 0; i < rowList.size(); i++) {
				DataRowBean dataRowBean = rowList.get(i);
				List<DataCellBean> cellList = dataRowBean.getDataCellList();
				String fieldName = (String)dataRowBean.getDataCellList().get(0).getData();
				String javaFieldName = StringUtil.getJavaStyleString(fieldName);
				String fieldType = (String)dataRowBean.getDataCellList().get(1).getData();
				String JavaTypeString = DBUtil.getJavaTypeNameByDB(fieldType);
				String comment = dataRowBean.getCommentByDataRowBean();
				columnStr+=("\n\t/**");
				columnStr+=("\n\t*"+comment);
				columnStr+=("\n\t**/\n");
				columnStr+=("\n\tprivate "+JavaTypeString+" "+javaFieldName+";");
				functionStr+=String.format("\n\n\t@Column(name=\""+fieldName+"\")");
				functionStr+=String.format("\n\t public "+JavaTypeString+" get"+StringUtil.firstUpper(javaFieldName)+"() {");
				functionStr+=String.format("\n\t\t%s","return this."+javaFieldName+";");
				functionStr+=String.format("\n\t}");
				functionStr+=String.format("\n\tpublic void set"+StringUtil.firstUpper(javaFieldName)+"("+JavaTypeString+" "+javaFieldName+") {");
				functionStr+=String.format("\n\t\t%s","changed("+javaFieldName+",this."+javaFieldName+");");
				;
				functionStr+=String.format("\n\t\t%s","this."+javaFieldName+" = "+javaFieldName+";");
				functionStr+=String.format("\n\t}");
				template +=String.format("\n\t// "+StringUtil.firstDowner(className)+".set"+StringUtil.firstUpper(javaFieldName)+"("+javaFieldName+")");
			}
			String finalString = headerStr +columnStr+functionStr+"\n"+template+"\n\n";
			finalString+="\t/**\n";
			finalString+="\t *系统生成代码和自定义代码的分隔符\n";
			finalString+="\t */\n";
			finalString+="}\n";
			File file2 = new File(FileUtil.getFileSourcePoPath(fileName+className));
			try {
				FileOutputStream fileOutputStream = new FileOutputStream(file2);
				fileOutputStream.write(finalString.getBytes("UTF-8"));
				fileOutputStream.close();
			} catch (IOException e) {
				ExceptionUtil.processException(e);
			}
			logger.debug(finalString);
		}


	}




}
